// pages/subpackage/pages/crm/codeManage/codeManage.js

// !!!!!!!!!!!!!!!!!!!!!!!!!!选择缴费器 和缴费器管理列表在一个页面   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

var app = getApp();
var _PAGE = 1;
var _PAGESIZE = 10;
let load = true;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showSeachIcon: true,  //搜索_____________框搜索图标
    keyword: "",          //搜索_____________搜索关键字内容

    dataList: [],//户号列表
    empty: true,
    hasMore: false,
    loading: false,
    loadShow: false,

    userid:'',//选择的用户id
    isChoose: false,//是否选择状态
  },

  /*** 生命周期函数--监听页面加载*/
  onLoad: function (options) {
    //选择户号 和户号管理列表在一个页面
    if (options.choose && options.userid) {
      wx.setNavigationBarTitle({
        title: '选择户号',
      })
      this.setData({
        isChoose: options.choose ? true : false,
        userid: options.userid,
      })
    }
  },
  /*** 生命周期函数--监听页面显示*/
  onShow: function () {
    var that = this;
    load = true;
    _PAGE = 1;
    that.setData({
      dataList: [],
    });
    that.getList();
  },
  //确认选择，返回上一页
  chooseSure(e) {
    var item = e.currentTarget.dataset.item;
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1]; // 当前页面
    var prevPage = pages[pages.length - 2]; // 上一级页面
    prevPage.setData({
      code_no: e.currentTarget.dataset.code,//选择的户号
    })
    wx.navigateBack();
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    _PAGE = 1
    that.getList();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var hasMore = that.data.hasMore;
    if (!hasMore) {
      return false;
    }
    _PAGE++;
    that.setData({
      loading: true
    })
    that.getList();
  },

  //获取用户户号列表
  getList() {
    var that = this;
    app.ajax({
      url: 'api/equipmentlists/getcodelists',
      url_type: 2,
      data: {
        page: _PAGE,
        pageSize: _PAGESIZE,
        userid: that.data.userid,
        code_no: that.data.keyword,
      },
      success: function (res) {
        wx.stopPullDownRefresh();
        if (res.data.code == 1000) {
          if (_PAGE == 1) {
            that.setData({
              dataList: res.data.data,
              empty: false,
            })
          } else {
            that.setData({
              dataList: that.data.dataList.concat(res.data.data),
              empty: false
            });
          }
          // 是否加载更多
          var hasMore = true;
          if (res.data.data.length < _PAGESIZE) {
            hasMore = false;
          }
          load = false;
          that.setData({
            hasMore: hasMore,
            loading: false
          });
        } else {
          if (_PAGE == 1) {
            that.setData({
              hasMore: false,
              loading: false,
              empty: true
            })
          } else {
            that.setData({
              hasMore: false,
              loading: false,
              empty: false
            })
          }
        }
        that.setData({
          loadShow: true
        })
      }
    });
  },

  //搜索_____________输入框输入内容
  searchInput(e) {
    this.setData({
      keyword: e.detail.value
    })
  },
  //搜索_____________只搜索编号
  onlyKeywordber() {
    _PAGE = 1;
    this.getList();
  },

  //跳转户号详情
  codeInfo(e) {
    wx.navigateTo({
      url: '../codeInfo/codeInfo?code_id=' + e.currentTarget.dataset.code_id,
    })
  },
  //跳转编辑户号
  codeEdit(e){
    wx.navigateTo({
      url: '../codeEdit/codeEdit?code_id=' + e.currentTarget.dataset.code_id,
    })
  },
})
